package yxy.game.pm2.module.active.progress;

import yxy.apple.util.redisson.RedisException;
import yxy.game.pm2.bean.db.dao.MySQLCacheDAO;
import yxy.game.pm2.bean.module.PlayerModule;
import yxy.game.pm2.module.tip.TipHandle;
import yxy.game.pm2.module.tip.TipType;

import java.util.ArrayList;

public class RankProgressPerson extends PlayerModule {

	// bean===========================================================================

	private int type;	//哪个排行榜
	private ArrayList<Integer> drawids = new ArrayList<Integer>();	//已经领取的进度奖励id
	
	public int getType() {
		return type;
	}
	public void setType(int type) {
		this.type = type;
	}
	public ArrayList<Integer> getDrawids() {
		return drawids;
	}
	public void setDrawids(ArrayList<Integer> drawids) {
		this.drawids = drawids;
	}
	
	// func==============================================================================
	
	private static RankProgressPerson create(String playerCode, int type) {
		RankProgressPerson module = new RankProgressPerson();
		module.setPlayerCode(playerCode);
		module.setType(type);
		module.set();
		return module;
	}
	
	// redis=============================================================================
	
	static public RankProgressPerson get(String playerCode, int type) {
		try {
			RankProgressPerson module = getv(key(playerCode, type));
			if (module == null) {
				module = MySQLCacheDAO.getV(key(playerCode,type), RankProgressPerson.class);
				if(module != null){module.set();}//同步到redis
			}

			if (module == null) {
				module = create(playerCode, type);
			}

			module.check();
			return module;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	//所有排行榜类型
	private static ArrayList<Integer> types(){
		ArrayList<Integer> types = new ArrayList<>();
		types.add(RankProgressArea.ConquestRank);
		types.add(RankProgressArea.TowerRank);
		types.add(RankProgressArea.PowerRank);
		return types;
	}

	private void check() {
		RankProgressArea areamodule = RankProgressArea.get(getPlayerCode(), getType());
		if(areamodule == null) {
			return;
		}
		
		TipType tt = null;
		if(getType() == RankProgressArea.ConquestRank) tt = TipType.排行榜_进度宝箱_征战;
		if(getType() == RankProgressArea.TowerRank) tt = TipType.排行榜_进度宝箱_修炼塔;
		if(getType() == RankProgressArea.PowerRank) tt = TipType.排行榜_进度宝箱_战力;
		
		boolean point = areamodule.getRpmap().size() > getDrawids().size();
		new TipHandle().updateTip(getPlayerCode(), tt, point);
	}
	
	
	private static String key(String playercode, int type) {
		return  String.format("PLAYER:%s:RANKPROGRESS:%s", playercode, type);
	}
	
	public void set() {
		try {
			setv(key(getPlayerCode(), getType()), this);
			check();
		} catch (RedisException e) {
			e.printStackTrace();
		}
	}

	//存储到数据库的方法
	public static void saveToDB(String playerCode) {
		try{
			for(Integer curType:types()) {
				String key = key(playerCode, curType);
				if (touchBucket(key).isExists()) {
					RankProgressPerson rankProgressPerson = RankProgressPerson.getv(key(playerCode, curType));
					MySQLCacheDAO.setV(key, rankProgressPerson);
				}
			}
		}catch (Exception e){
			e.printStackTrace();
		}
	}

	//从redis移走数据的操作
	public static void removeAllFromRedis(String playerCode) {
		try {
			for (Integer curType:types()){
				touchBucket(key(playerCode,curType)).delete();
			}
		} catch (RedisException e) {
			e.printStackTrace();
		}
	}

	// lock==============================================================================
	
	@Override
	public String lockKey() {
		// TODO Auto-generated method stub
		return null;
	}

}
